<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./js/api.js"></script>
    <script src="./js/init.js"></script>


    <table>
        <div>添加博客或修改</div>
        <tr>
            <a>标题:</a>
            <input type="text" name="biaoti">
        </tr>
        <br>
        <tr>
            <a>摘要:</a>
            <input type="text" name="zhaiyao">
        </tr>
        <br>
        <tr>
            <a>内容:</a>
            <input type="text" name="text">
        </tr>
        <br>
        <tr>
            <a>分类:</a>
            <input type="text" name="type">
        </tr>
        <br>
        <tr>
            <a>作者:</a>
            <input type="text" name="author">
        </tr>
        <br>
        <tr>
            <a>创建时间:</a>
            <input type="text" name="createdAt">
        </tr>
        <br>
        <tr>
            <input type="button" value="修改" onclick="save()">
            <input type="button" value="取消" onclick="cancel()">
        </tr>
        <br>
    </table>
    <script>
$(function () {
    
    let id=geturl('id')
    id=id||0
    getbyid(id).then(res=>{
        let obj=res.data
        $('[name=biaoti]').val(obj.biaoti)
        $('[name=zhaiyao]').val(obj.zhaiyao)
        $('[name=text]').val(obj.text)
        $('[name=type]').val(obj.type)
        $('[name=author]').val(obj.author)
        $('[name=createdAt]').val(obj.createdAt) 
    })
})
function geturl(name) {
    var reg=new RegExp("(^|&)"+name+"=([^&])(&|$)")
    var r=window.location.search.substr(1).match(reg)
    if(r!==null)
    return unescape(r[2])
    return null
    
}

function save() {
    let id=geturl('id')
    let obj={
        biaoti:$('[name=biaoti]').val(),
        zhaiyao: $('[name=zhaiyao]').val(),
        text:$('[name=text]').val(),
        type: $('[name=type]').val(),
        author:$('[name=author]').val(),
        createdAt:$('[name=createdAt]').val() 
    }
    if (id) {
        putlist(id,obj).then(res=>{
            console.log(res);
            alert('修改成功')
            window.location.href='index.html'
        })
    }
    else{
        postlist(obj).then(res=>{
            console.log(res);
            alert('添加成功')
            window.location.href='index.html'
        })

    }
}
function cancel() {
    window.location.href='index.html'
}
    </script> 
</body>
</html>